var ip = 'http://' + document.location.host;
var status = $(".status").val();
var sfunction = $(".sfunction").val();
var fymc = $(".fymc").val();

var tip_index = 0;
$("table").on("mouseover", "td", function() {
    if(this.offsetWidth < this.scrollWidth) { //判断文本是否超出
        var content = $(this).text(); //获取文案
        if(content) {
            tip_index = layer.tips(content, this, { time: 0 , tips: [1, '#4682B4'], area: ['200px', 'auto']});
        }
    }  
})

$("table").on("mouseleave",  "td", function() {
    layer.close(tip_index);
})


$.ajax({//初始化下拉列表
	url : "/subscribe/findAllSubscribe",
	type : "GET",
	dataType : "json",
	success : function(res) {
		if (res.code == 200) {
			
			$(".sfunction").empty(); // 清空
			$(".sfunction").append("<option value=''>全部</option>");
			for (var i = 0; i < res.data.length; i++) {
				$(".sfunction").append("<option value='"+res.data[i].functionName+"'>"+res.data[i].functionName+"</option>");
				/*$(".sfunction").append(
						$("<option/>").text(res.data[i].functionName).attr("value",res.data[i].id)); // 动态添加标签
*/
			}
		}
	}
})

// 查询用户
$('.btn_search').on('click',function() {
	status = $(".status").val();
	fymc = $(".fymc").val();
	sfunction = $(".sfunction").val();
	var table = new Table('Tbody');
	table.init();
});

//清除筛选条件
$('.btn_search_clean').on('click',function() {
	$(".status").val("");
    $(".fymc").val("");
    $(".sfunction").val("");
});

$(function() {
    hasLogin(); //是否登陆
    var table = new Table('Tbody');
    table.init();

    //header  Tab
    $('#gonggaoManagement').on('click', function() {
        window.location.href = 'gonggao_manage.html';
    });
    $('#memManagement').on('click', function() {
        window.location.href = 'user_manage.html';
    });
    $('#reportManagement').on('click', function() {
        window.location.href = 'report_manage.html';
    });
    $('#messageManagement').on('click', function() {
        window.location.href = 'message_manage.html';
    });
    $('#subscribeManagement').on('click', function() {
        window.location.href = 'subscribe_function.html';
    });
    $('#subscribeFunction').on('click', function() {
        window.location.href = 'subscribe_function.html';
    });
    $('#subscribeCourt').on('click', function() {
        window.location.href = 'subscribe_court.html';
    });
    $('#subscribeOpen').on('click', function() {
        window.location.href = 'subscribe_open.html';
    });

    //退出
    $('.btn_esc').on('click', function() {
        window.sessionStorage.removeItem('MANAGER');
        window.location.href = 'login_manage.html'
    });
})

//是否登录
var hasLogin = function() {
    var username = window.sessionStorage.getItem('MANAGER');
    if (!username) window.location.href = 'login_manage.html'
}

//Table 
function Table(obj_classname) {
    this.$obj = $('.' + obj_classname)
}

Table.prototype = {
    constructor: Table,
    init: function() {
        this._bind();
    },
    _bind: function() {
        this._create(); //创建table modal
        //this._closeModal(); //关闭模态框
    },
    _create: function() {
        var that = this;
        $.ajax({
            type: 'post',
            url: ip + '/subscribe/page?offset=0&limit=10',
            data:{"status":status,"fymc":fymc,"sfunction":sfunction},
            dataType: 'json',
            // contentType: 'application/json',
            success: function(res) {
                // console.log(res)
                //if (!res.count) return false;
                var totalData = res.count;
                $('.total_num').text(totalData);
                that._page(totalData);
            },
            error: function(error) {
                // console.log(error);
            }
        })
    },
    _page: function(totalData) {
        var that = this;
        $('.page_s').pagination({
            totalData: totalData,
            showData: 10,
            pageCount: ~~(totalData / 10) + 1,
            count: 2,
            isHide: false,
            keepShowPN: true,
            coping: true, //首页和尾页
            prevContent: '〈',
            nextContent: '〉',
            callback: function(api) {
                that._render((api.getCurrent() - 1) * 10)
                // console.log(api.getCurrent() - 1);
            }
        }, function(api) { //回调函数
            that._render(api.getCurrent() - 1);
            // console.log(api.getCurrent() - 1);
        })
    },
    _render: function(index) {
        var that = this;
        $.ajax({
            type: 'post',
            url: ip + '/subscribe/page?offset=' + index + '&limit=10',
            data:{"status":status,"fymc":fymc,"sfunction":sfunction},
            dataType: 'json',
            success: function(res) {
                //多次判断有无数据
                // console.log(res);
                //if (!res.count) return false;
                var data = res.queryData;
                that._table(data, index);
                //that._modal(data);
            },
            error: function(error) {
                // console.log(error);
            }
        })
    },
    _table: function(data, index) {
        // console.log(data);
        var html = '';
        for (var i = 0, l = data.length; i < l; i++) {
        	var tmp = data[i];
        	if(tmp.sfunction=='其他功能'){
        		html +=
                    '<tr>' +
                    '<td class="test">' + (i + 1 + index) + '</td>' +
                    '<td>' + tmp.ip + '</td>' +
                    '<td>' + tmp.name + '</td>' +
                    '<td>' + tmp.fymc + '</td>' +
                    '<td>' + tmp.sfunction + '</td>' +
                    '<td>' + tmp.message + '</td>' +
                    '<td>' + tmp.status + '</td>' +
                    '<td>' + tmp.phone + '</td>' +
                    '<td>' + tmp.email + '</td>' +
                    '<td>' + tmp.created + '</td>' +
                    '<td></td>'
                    '</tr>';
        	}else{
        		if(tmp.status=='开通'){
            		html +=
                        '<tr>' +
                        '<td class="test">' + (i + 1 + index) + '</td>' +
                        '<td>' + tmp.ip + '</td>' +
                        '<td>' + tmp.name + '</td>' +
                        '<td>' + tmp.fymc + '</td>' +
                        '<td>' + tmp.sfunction + '</td>' +
                        '<td>' + tmp.message + '</td>' +
                        '<td>' + tmp.status + '</td>' +
                        '<td>' + tmp.phone + '</td>' +
                        '<td>' + tmp.email + '</td>' +
                        '<td>' + tmp.created + '</td>' +
                        '<td class="tab_operate"><a href="javascript:void(0);" onclick ="updateGg(\''+tmp.id+'\',\''+tmp.status+'\')"><span class="btn_delete"><i class="fa fa-ban fa-lg"></i>关闭</span></a></td>'
                        '</tr>';
            	}else{
            		html +=
                        '<tr>' +
                        '<td class="test">' + (i + 1 + index) + '</td>' +
                        '<td>' + tmp.ip + '</td>' +
                        '<td>' + tmp.name + '</td>' +
                        '<td>' + tmp.fymc + '</td>' +
                        '<td>' + tmp.sfunction + '</td>' +
                        '<td>' + tmp.message + '</td>' +
                        '<td>' + tmp.status + '</td>' +
                        '<td>' + tmp.phone + '</td>' +
                        '<td>' + tmp.email + '</td>' +
                        '<td>' + tmp.created + '</td>' +
                        '<td class="tab_operate"><a href="javascript:void(0);" onclick ="updateGg(\''+tmp.id+'\',\''+tmp.status+'\')"><span class="btn_delete"><i class="fa fa-check-square fa-lg"></i>开通</span></a></td>'
                        '</tr>';
            	}
        	}
        	
        	
        	
        }
        this.$obj.html(html);
    }
}
function updateGg(id,status){
	if(status=='开通'){
		layer.confirm('您确定要为用户关闭此功能吗？', {btn : [ '确定', '取消' ]}, 
				function(){
					layer.closeAll('dialog');
					$.ajax({
						type: 'POST',
					    url: ip + '/subscribe/updateSubscribeStatus?id='+id+'&status=关闭',
						// 当我们想服务器发送的请求参数是一个json的时候，必须设置contentType:application/json
						//contentType : 'application/json',
					    //dataType: 'text',
					    success: function(data) {
					    	if(data.code == 200){
					    		var table = new Table('Tbody');
					    		table.init();
					    	}else{
					    		layer.alert(data.msg, {icon: 5,title: "提示"});
					    	}
					    }
					});

				});
	}else{
		layer.confirm('您确定要为用户开通此功能吗？', {btn : [ '确定', '取消' ]}, 
				function(){
					layer.closeAll('dialog');
					$.ajax({
						type: 'POST',
					    url: ip + '/subscribe/updateSubscribeStatus?id='+id+'&status=开通',
						// 当我们想服务器发送的请求参数是一个json的时候，必须设置contentType:application/json
						//contentType : 'application/json',
					    //dataType: 'text',
					    success: function(data) {
					    	if(data.code == 200){
					    		var table = new Table('Tbody');
					    		table.init();
					    	}else{
					    		layer.alert(data.msg, {icon: 5,title: "提示"});
					    	}
					    }
					});

				});
	}
	
	
}


